import type { Ref } from "vue"
import type { Filters, ResourceDetail, SearchCondition } from "../models/Resource"
import { loadResourceDetailAPI, loadResourcePaceAPI } from "../apis/ResourceAPI"

export const handelLoadResourcePage = async (query: string, finalFilters: Filters, pageIndex: number, isSearch: boolean) => {
    console.log(`搜索关键词:${query}, 筛选条件:${JSON.stringify(finalFilters)}, 加载第 ${pageIndex} 页的数据...`)
    //封装查询条件 
    const searchCondition: SearchCondition = {
        isSearch: isSearch,
        query: query,
        filters: finalFilters,
        pageIndex: pageIndex
    };

    const result = await loadResourcePaceAPI(searchCondition);
    if (result.code !== 0) {
        throw new Error("无法加载资源页面");
    }
    return result.data
}

export const handelLoadResourceDetail = async (resource: Ref<ResourceDetail>) => {

    console.log('查看资源细节id:', resource.value.id)
    const result = await loadResourceDetailAPI(resource.value.id)
    if (result.code !== 0) {
        throw new Error("无法加载资源细节")
    }
    resource.value = result.data as ResourceDetail
}